package com.oocl.o2o.client.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oocl.o2o.client.dao.CommentaryDao;
import com.oocl.o2o.client.dao.MerchantDao;
import com.oocl.o2o.client.entity.Commentary;
import com.oocl.o2o.client.entity.Customer;
import com.oocl.o2o.client.entity.Merchant;
import com.oocl.o2o.client.service.CommentaryService;
import com.oocl.o2o.client.vo.CommentaryVO;
import com.oocl.o2o.client.vo.EvaluateVO;

@Service(value="commentaryServiceImpl")
public class CommentaryServiceImpl implements CommentaryService {

	@Resource(name="commentaryDaoImpl")
	private CommentaryDao commentaryDao;
	@Resource
	private MerchantDao merchantDao;
	@Override
	@Transactional
	public Commentary add(EvaluateVO evaluateVO,Customer customer) {
		Commentary commentary = new Commentary();
		Merchant merchant = merchantDao.findByUsername(evaluateVO.getMerchantName());
		commentary.setCustomer(customer);
		commentary.setCustomerCommentary(evaluateVO.getCommentary());
		commentary.setCustomerCommentaryDate(new Date());
		commentary.setGrade(evaluateVO.getGrade());
		commentary.setMerchant(merchant);
		
		return commentaryDao.add(commentary);
	}

	@Override
	public List<Commentary> findCommentaryByCustomerId(String id) {


		return commentaryDao.findCommentaryByCustomerId(id);
	}

	@Override
	public List<CommentaryVO> findCommentaryByMerchantName(String name) {
		List<Commentary> commentaries = new ArrayList<Commentary>();
		List<CommentaryVO> commentaryVOs = new ArrayList<CommentaryVO>();
		commentaries = commentaryDao.findCommentaryByMerchantName(name);
		for(Commentary c:commentaries){
			CommentaryVO commentaryVO = new CommentaryVO();
			commentaryVO.setId(c.getId());
			commentaryVO.setCustomerCommentary(c.getCustomerCommentary());
			commentaryVO.setCustomerCommentaryDate(c.getCustomerCommentaryDate());
			commentaryVO.setCustomerName(c.getCustomer().getUsername());
			commentaryVO.setMerchantName(c.getMerchant().getUsername());
			commentaryVO.setGrade(c.getGrade());
			commentaryVO.setMerchantCommentary(c.getMerchantCommentary());
			commentaryVO.setMerchantCommentaryDate(c.getMerchantCommentaryDate());
			commentaryVOs.add(commentaryVO);
		}
		
		return commentaryVOs ;
	}

	@Override
	@Transactional
	public Commentary update(Commentary commentary) {
		
		return commentaryDao.update(commentary);
	}

	@Override
	public Commentary findById(String id) {
		// TODO Auto-generated method stub
		return commentaryDao.findById(id);
	}

}
